{
 "nbformat": 4,
 "nbformat_minor": 2,
 "metadata": {
  "language_info": {
   "name": "python",
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "version": "3.8.1-final"
  },
  "orig_nbformat": 2,
  "file_extension": ".py",
  "mimetype": "text/x-python",
  "name": "python",
  "npconvert_exporter": "python",
  "pygments_lexer": "ipython3",
  "version": 3,
  "kernelspec": {
   "name": "python38164bitc9425b4fd2d44566b8cda82b00a1f4f8",
   "display_name": "Python 3.8.1 64-bit"
  }
 },
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd \n",
    "import numpy as np \n",
    "import scipy.spatial.distance as ssd \n",
    "from collections import defaultdict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>item_id</th>\n      <th>rating</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n      <td>1</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>1</td>\n      <td>2</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>1</td>\n      <td>3</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>1</td>\n      <td>4</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>1</td>\n      <td>5</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>79995</th>\n      <td>943</td>\n      <td>1067</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>79996</th>\n      <td>943</td>\n      <td>1074</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>79997</th>\n      <td>943</td>\n      <td>1188</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>79998</th>\n      <td>943</td>\n      <td>1228</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>79999</th>\n      <td>943</td>\n      <td>1330</td>\n      <td>3</td>\n    </tr>\n  </tbody>\n</table>\n<p>80000 rows × 3 columns</p>\n</div>",
      "text/plain": "       user_id  item_id  rating\n0            1        1       5\n1            1        2       3\n2            1        3       4\n3            1        4       3\n4            1        5       3\n...        ...      ...     ...\n79995      943     1067       2\n79996      943     1074       4\n79997      943     1188       3\n79998      943     1228       3\n79999      943     1330       3\n\n[80000 rows x 3 columns]"
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_train = pd.read_csv(\n",
    "    './movielen_rating_training.base',\n",
    "    names = ['user_id', 'item_id', 'rating'],\n",
    "    usecols = [0,1,2],\n",
    "    sep = '\\t'\n",
    ")\n",
    "df_train"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [],
   "source": [
    "user_id_uique = df_train.user_id.unique()\n",
    "item_id_uique = df_train.item_id.unique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [],
   "source": [
    "user_index_map = {}\n",
    "item_index_map  = {}\n",
    "for i in range(len(user_id_uique)):\n",
    "   user_index_map[user_id_uique[i]] = i\n",
    "for i in range(len(item_id_uique)):\n",
    "    item_index_map[item_id_uique[i]] = i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "user_item_rating_array = np.zeros(shape = (len(user_id_uique),len(item_id_uique)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "array([[5., 3., 4., ..., 0., 0., 0.],\n       [4., 0., 0., ..., 0., 0., 0.],\n       [0., 0., 0., ..., 0., 0., 0.],\n       ...,\n       [5., 0., 0., ..., 0., 0., 0.],\n       [0., 0., 0., ..., 0., 0., 0.],\n       [0., 5., 0., ..., 0., 0., 0.]])"
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_rating_map = defaultdict(set)\n",
    "\n",
    "for index in df_train.index:\n",
    "    data = df_train.iloc[index]\n",
    "    user_index = user_index_map[data['user_id']]\n",
    "    item_index =  item_index_map[data['item_id']]\n",
    "    user_rating_map[user_index].add(item_index)\n",
    "    user_item_rating_array[user_index, item_index] = data.rating \n",
    "user_item_rating_array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "[3.6814814814814816,\n 3.8,\n 3.0,\n 4.357142857142857,\n 2.956043956043956,\n 3.581818181818182,\n 3.892018779342723,\n 3.6,\n 4.166666666666667,\n 4.212765957446808,\n 3.533333333333333,\n 4.28,\n 3.136729222520107,\n 4.219512195121951,\n 3.033333333333333,\n 4.3478260869565215,\n 3.1578947368421053,\n 3.9371069182389937,\n 3.6,\n 3.3076923076923075,\n 2.663157894736842,\n 3.3,\n 3.6363636363636362,\n 4.390243902439025,\n 4.048780487804878,\n 2.909090909090909,\n 3.3,\n 3.641025641025641,\n 3.9411764705882355,\n 3.8,\n 3.9130434782608696,\n 3.5454545454545454,\n 3.642857142857143,\n 3.8,\n 3.0,\n 4.0,\n 3.4594594594594597,\n 3.9047619047619047,\n 3.8461538461538463,\n 2.727272727272727,\n 3.935483870967742,\n 3.625,\n 3.6785714285714284,\n 3.6375,\n 3.4827586206896552,\n 4.181818181818182,\n 3.5625,\n 3.727272727272727,\n 2.7289719626168223,\n 3.5384615384615383,\n 3.75,\n 4.3428571428571425,\n 4.0,\n 3.3333333333333335,\n 3.8,\n 3.652173913043478,\n 3.6296296296296298,\n 3.875,\n 4.023255813953488,\n 4.1344537815126055,\n 2.8333333333333335,\n 3.317073170731707,\n 2.9791666666666665,\n 3.596330275229358,\n 3.9791666666666665,\n 3.5238095238095237,\n 3.4166666666666665,\n 3.1666666666666665,\n 3.736842105263158,\n 3.4025974025974026,\n 3.8181818181818183,\n 3.767123287671233,\n 3.6470588235294117,\n 3.761904761904762,\n 3.1818181818181817,\n 3.4,\n 3.4615384615384617,\n 3.357142857142857,\n 4.185185185185185,\n 3.9285714285714284,\n 3.566666666666667,\n 3.127906976744186,\n 3.3974358974358974,\n 3.744186046511628,\n 3.547169811320755,\n 3.6363636363636362,\n 3.7787610619469025,\n 4.090909090909091,\n 4.15,\n 4.25,\n 3.9,\n 3.230392156862745,\n 3.0,\n 3.660633484162896,\n 3.484076433121019,\n 4.25,\n 3.9714285714285715,\n 3.875,\n 3.6507936507936507,\n 3.0294117647058822,\n 3.0,\n 2.5625,\n 3.4,\n 2.7936507936507935,\n 3.3125,\n 3.7142857142857144,\n 2.769230769230769,\n 3.411764705882353,\n 3.5,\n 3.0853658536585367,\n 3.5,\n 3.9166666666666665,\n 3.806451612903226,\n 3.6296296296296298,\n 4.040816326530612,\n 3.0568181818181817,\n 3.7222222222222223,\n 4.642857142857143,\n 3.877551020408163,\n 3.5714285714285716,\n 3.6739130434782608,\n 3.9705882352941178,\n 3.9310344827586206,\n 3.5,\n 3.4444444444444446,\n 3.6956521739130435,\n 4.181818181818182,\n 3.5784313725490198,\n 2.8666666666666667,\n 4.078651685393258,\n 4.066666666666666,\n 3.7058823529411766,\n 3.2777777777777777,\n 3.5833333333333335,\n 3.45,\n 4.55,\n 4.083333333333333,\n 4.296296296296297,\n 3.933333333333333,\n 3.6,\n 3.4461538461538463,\n 3.8,\n 3.7333333333333334,\n 3.6666666666666665,\n 3.3189189189189188,\n 3.6842105263157894,\n 4.3,\n 4.0,\n 3.0,\n 3.8,\n 4.0843373493975905,\n 4.285714285714286,\n 3.076923076923077,\n 3.962962962962963,\n 2.6363636363636362,\n 3.7777777777777777,\n 3.7037037037037037,\n 3.8383838383838382,\n 3.4057971014492754,\n 4.090909090909091,\n 2.5294117647058822,\n 3.35,\n 3.076923076923077,\n 4.0606060606060606,\n 4.0,\n 3.3,\n 3.8157894736842106,\n 3.257142857142857,\n 3.9565217391304346,\n 3.6666666666666665,\n 3.4210526315789473,\n 3.0,\n 4.185185185185185,\n 3.8020833333333335,\n 3.8260869565217392,\n 3.642857142857143,\n 3.7301587301587302,\n 3.718954248366013,\n 2.9130434782608696,\n 3.8529411764705883,\n 1.518348623853211,\n 4.142857142857143,\n 3.1666666666666665,\n 3.7142857142857144,\n 3.875,\n 3.3333333333333335,\n 4.205882352941177,\n 3.896551724137931,\n 3.963302752293578,\n 3.388888888888889,\n 4.0,\n 3.45,\n 3.278688524590164,\n 2.9444444444444446,\n 3.2244897959183674,\n 3.238095238095238,\n 3.1923076923076925,\n 3.2547169811320753,\n 3.1578947368421053,\n 4.0,\n 3.144186046511628,\n 2.8461538461538463,\n 3.272727272727273,\n 3.6315789473684212,\n 2.8,\n 1.878787878787879,\n 3.218045112781955,\n 3.727272727272727,\n 3.375,\n 3.9857142857142858,\n 3.380952380952381,\n 4.235294117647059,\n 4.2388059701492535,\n 3.873015873015873,\n 3.86,\n 3.9315068493150687,\n 3.054054054054054,\n 3.606060606060606,\n 4.125,\n 3.8,\n 3.6144578313253013,\n 3.032863849765258,\n 3.1923076923076925,\n 3.09375,\n 4.8,\n 3.6451612903225805,\n 3.2777777777777777,\n 2.857142857142857,\n 3.1333333333333333,\n 3.92,\n 3.6,\n 3.893617021276596,\n 4.352941176470588,\n 3.1043165467625897,\n 3.893617021276596,\n 3.357142857142857,\n 4.111111111111111,\n 3.2142857142857144,\n 4.116279069767442,\n 3.6,\n 3.3333333333333335,\n 4.2727272727272725,\n 3.5555555555555554,\n 3.7094017094017095,\n 3.3076923076923075,\n 2.954954954954955,\n 3.8,\n 3.1904761904761907,\n 4.191011235955056,\n 3.830769230769231,\n 3.727272727272727,\n 4.545454545454546,\n 3.9464285714285716,\n 3.152941176470588,\n 2.4782608695652173,\n 4.069565217391304,\n 4.258064516129032,\n 3.625,\n 3.814814814814815,\n 4.166666666666667,\n 4.1,\n 3.207317073170732,\n 4.05,\n 4.190476190476191,\n 3.6296296296296298,\n 3.1875,\n 3.990566037735849,\n 2.9329608938547485,\n 2.8502994011976046,\n 4.291666666666667,\n 3.590062111801242,\n 4.387096774193548,\n 3.5,\n 4.0256410256410255,\n 3.0625,\n 3.4160839160839163,\n 3.2857142857142856,\n 4.166666666666667,\n 3.1735537190082646,\n 3.6530612244897958,\n 3.2941176470588234,\n 3.4,\n 4.071428571428571,\n 3.607142857142857,\n 3.8125,\n 3.8354430379746836,\n 3.857142857142857,\n 3.7209302325581395,\n 2.6153846153846154,\n 3.4177215189873418,\n 3.69364161849711,\n 4.142857142857143,\n 3.097674418604651,\n 3.5,\n 4.29,\n 4.256756756756757,\n 3.4086021505376345,\n 4.1923076923076925,\n 3.5064935064935066,\n 3.75,\n 3.589403973509934,\n 2.5,\n 3.3659420289855073,\n 3.4615384615384617,\n 3.424,\n 3.3333333333333335,\n 3.6666666666666665,\n 3.834862385321101,\n 3.8181818181818183,\n 3.0,\n 3.7567567567567566,\n 4.484615384615385,\n 3.6470588235294117,\n 3.6714285714285713,\n 3.977777777777778,\n 3.1136363636363638,\n 3.5833333333333335,\n 3.6875,\n 3.25,\n 3.7341772151898733,\n 3.8194444444444446,\n 4.222222222222222,\n 3.6363636363636362,\n 4.392156862745098,\n 3.3552631578947367,\n 3.2212389380530975,\n 3.28,\n 3.408284023668639,\n 3.2972972972972974,\n 4.447058823529412,\n 3.65,\n 4.137931034482759,\n 3.7857142857142856,\n 3.532110091743119,\n 3.6153846153846154,\n 3.011764705882353,\n 4.0476190476190474,\n 4.121951219512195,\n 4.063291139240507,\n 4.111111111111111,\n 3.6666666666666665,\n 3.4765625,\n 3.9872611464968153,\n 3.697247706422018,\n 3.6641791044776117,\n 3.328358208955224,\n 3.603305785123967,\n 3.774193548387097,\n 3.357142857142857,\n 4.4,\n 4.133333333333334,\n 3.793103448275862,\n 3.2857142857142856,\n 3.7171052631578947,\n 4.1,\n 3.466666666666667,\n 4.0576923076923075,\n 3.7142857142857144,\n 4.142857142857143,\n 3.971014492753623,\n 3.7564102564102564,\n 3.2777777777777777,\n 3.0861244019138754,\n 3.357142857142857,\n 3.3684210526315788,\n 4.352941176470588,\n 4.114285714285714,\n 3.3125,\n 4.0,\n 3.6785714285714284,\n 4.102564102564102,\n 4.2272727272727275,\n 3.717948717948718,\n 3.3195876288659796,\n 3.9375,\n 3.6363636363636362,\n 4.0,\n 3.4053030303030303,\n 4.014705882352941,\n 3.163793103448276,\n 3.797752808988764,\n 3.5454545454545454,\n 4.423076923076923,\n 4.1875,\n 3.3069306930693068,\n 3.8333333333333335,\n 3.3333333333333335,\n 4.119047619047619,\n 3.707920792079208,\n 3.75,\n 3.7578947368421054,\n 4.074074074074074,\n 3.33968253968254,\n 3.808,\n 3.8260869565217392,\n 3.3076923076923075,\n 3.8375,\n 3.623076923076923,\n 2.92,\n 3.6470588235294117,\n 3.1150442477876106,\n 3.8333333333333335,\n 3.6382978723404253,\n 3.25,\n 1.8487972508591066,\n 3.4744525547445257,\n 3.478723404255319,\n 3.782608695652174,\n 3.703225806451613,\n 3.0869565217391304,\n 3.74,\n 3.945945945945946,\n 3.675,\n 3.9545454545454546,\n 4.285714285714286,\n 3.8513189448441247,\n 3.2343234323432344,\n 2.8666666666666667,\n 4.172413793103448,\n 4.21875,\n 3.8181818181818183,\n 3.4125,\n 3.5294117647058822,\n 3.3,\n 2.943820224719101,\n 3.8160919540229883,\n 4.583333333333333,\n 3.9375,\n 3.370879120879121,\n 3.5081967213114753,\n 3.2777777777777777,\n 3.6666666666666665,\n 3.4594594594594597,\n 3.731707317073171,\n 3.30835734870317,\n 3.746031746031746,\n 3.5714285714285716,\n 4.068965517241379,\n 3.7142857142857144,\n 4.145833333333333,\n 3.5,\n 3.1451612903225805,\n 3.375,\n 3.7142857142857144,\n 2.0081967213114753,\n 2.742857142857143,\n 3.574803149606299,\n 3.3714285714285714,\n 3.746268656716418,\n 3.8582995951417005,\n 2.7362637362637363,\n 3.438775510204082,\n 3.2751677852348995,\n 2.973333333333333,\n 3.5,\n 3.4694835680751175,\n 4.026022304832714,\n 3.683333333333333,\n 3.3846153846153846,\n 3.417910447761194,\n 3.0,\n 3.96875,\n 2.8646616541353382,\n 4.037735849056604,\n 3.4375,\n 3.4653465346534653,\n 3.6818181818181817,\n 3.993006993006993,\n 4.534883720930233,\n 3.5789473684210527,\n 3.3870967741935485,\n 4.228136882129277,\n 3.914285714285714,\n 4.08256880733945,\n 3.6,\n 3.3734939759036147,\n 4.457142857142857,\n 3.415929203539823,\n 3.4207920792079207,\n 3.7333333333333334,\n 3.982142857142857,\n 3.423076923076923,\n 3.0508474576271185,\n 3.9928057553956835,\n 3.0384615384615383,\n 3.2222222222222223,\n 3.645021645021645,\n 3.357142857142857,\n 3.7247706422018347,\n 2.789473684210526,\n 3.8484848484848486,\n 3.5272727272727273,\n 3.7555555555555555,\n 3.872340425531915,\n 3.9530516431924885,\n 3.0310077519379846,\n 3.1792114695340503,\n 3.2885906040268456,\n 3.8627450980392157,\n 3.3422222222222224,\n 3.767123287671233,\n 3.108108108108108,\n 4.01875,\n 3.6215139442231075,\n 3.3660714285714284,\n 3.6735537190082646,\n 4.724137931034483,\n 3.8275862068965516,\n 2.515151515151515,\n 2.7666666666666666,\n 3.75,\n 4.333333333333333,\n 4.363636363636363,\n 3.8402061855670104,\n 3.0,\n 4.095238095238095,\n 3.2972972972972974,\n 3.643835616438356,\n 4.122448979591836,\n 3.4347826086956523,\n 3.14,\n 4.333333333333333,\n 4.36,\n 3.4934640522875817,\n 3.2962962962962963,\n 3.235294117647059,\n 3.832116788321168,\n 3.7169811320754715,\n 3.604651162790698,\n 3.888888888888889,\n 3.2333333333333334,\n 4.12043795620438,\n 3.3846153846153846,\n 4.075,\n 3.9357798165137616,\n 3.871165644171779,\n 2.8653061224489798,\n 3.617283950617284,\n 3.982142857142857,\n 3.7142857142857144,\n 3.6240601503759398,\n 3.488888888888889,\n 3.532994923857868,\n 2.806451612903226,\n 3.506172839506173,\n 3.8983050847457625,\n 3.652173913043478,\n 3.670967741935484,\n 3.72,\n 3.675,\n 3.802395209580838,\n 3.0952380952380953,\n 4.17,\n 3.5701754385964914,\n 4.019230769230769,\n 4.204545454545454,\n 3.7547169811320753,\n 4.2,\n 3.5789473684210527,\n 3.396039603960396,\n 3.0252100840336134,\n 3.5416666666666665,\n 3.7666666666666666,\n 3.411764705882353,\n 4.542857142857143,\n 3.442953020134228,\n 3.9548387096774196,\n 3.3703703703703702,\n 3.4857142857142858,\n 2.6818181818181817,\n 3.45,\n 3.25,\n 3.7450980392156863,\n 3.5217391304347827,\n 3.1538461538461537,\n 3.5277777777777777,\n 3.8031914893617023,\n 2.6666666666666665,\n 3.472972972972973,\n 3.5531914893617023,\n 4.035714285714286,\n 3.274193548387097,\n 4.444444444444445,\n 3.5416666666666665,\n 3.7375,\n 3.325301204819277,\n 2.9693877551020407,\n 3.7422222222222223,\n 3.441860465116279,\n 3.5,\n 3.6547619047619047,\n 3.813888888888889,\n 3.608974358974359,\n 3.48,\n 3.1979166666666665,\n 3.6,\n 3.6341463414634148,\n 3.68,\n 3.893617021276596,\n 3.5842696629213484,\n 3.081081081081081,\n 3.793103448275862,\n 3.7872340425531914,\n 3.2962962962962963,\n 3.7555555555555555,\n 3.9661016949152543,\n 3.8378378378378377,\n 3.6748466257668713,\n 2.392857142857143,\n 3.7333333333333334,\n 3.8333333333333335,\n 3.3703703703703702,\n 4.321428571428571,\n 3.128205128205128,\n 3.8640776699029127,\n 3.627906976744186,\n 2.6203703703703702,\n 3.287037037037037,\n 3.50561797752809,\n 3.827272727272727,\n 3.5497076023391814,\n 3.3903508771929824,\n 3.7333333333333334,\n 3.5319148936170213,\n 3.4339622641509435,\n 2.34375,\n 3.292817679558011,\n 4.703703703703703,\n 4.082644628099174,\n 3.324074074074074,\n 3.1,\n 3.6610169491525424,\n 3.310344827586207,\n 3.3703703703703702,\n 3.264705882352941,\n 4.45,\n 2.4903846153846154,\n 3.537313432835821,\n 2.9594594594594597,\n 4.229357798165138,\n 4.125,\n 3.6226415094339623,\n 3.737864077669903,\n 4.0,\n 3.959016393442623,\n 2.9473684210526314,\n 3.8275862068965516,\n 3.259259259259259,\n 3.7916666666666665,\n 3.1543408360128615,\n 3.2857142857142856,\n 3.1363636363636362,\n 2.6925795053003534,\n 3.687074829931973,\n 2.908029197080292,\n 2.5416666666666665,\n 3.2413793103448274,\n 3.732394366197183,\n 3.7905759162303663,\n 2.5848214285714284,\n 3.9173553719008263,\n 3.869565217391304,\n 3.5316455696202533,\n 3.746987951807229,\n 3.4788732394366195,\n 3.6653061224489796,\n 3.9574468085106385,\n 3.5217391304347827,\n 3.38,\n 3.847826086956522,\n 3.5806451612903225,\n 3.2413793103448274,\n 3.8857142857142857,\n 3.658536585365854,\n 3.7058823529411766,\n 3.5844155844155843,\n 3.5918367346938775,\n 3.4285714285714284,\n 3.661290322580645,\n 4.0,\n 3.227272727272727,\n 3.137844611528822,\n 3.2266666666666666,\n 3.488372093023256,\n 2.05,\n 4.563380281690141,\n 3.4761904761904763,\n 4.833333333333333,\n 3.8333333333333335,\n 3.1478260869565218,\n 4.21875,\n 3.275,\n 3.1363636363636362,\n 4.1528662420382165,\n 3.289473684210526,\n 3.9310344827586206,\n 3.7142857142857144,\n 2.84251968503937,\n 3.1476510067114094,\n 3.6666666666666665,\n 4.212121212121212,\n 2.4516129032258065,\n 3.5319148936170213,\n 3.7777777777777777,\n 3.710526315789474,\n 3.310344827586207,\n 3.4957627118644066,\n 3.2,\n 3.572463768115942,\n 3.697674418604651,\n 3.8597285067873304,\n 3.765432098765432,\n 2.966666666666667,\n 3.522727272727273,\n 3.3832335329341316,\n 3.8884758364312266,\n 3.7096774193548385,\n 3.6923076923076925,\n 3.1044776119402986,\n 3.966666666666667,\n 3.5357142857142856,\n 3.619047619047619,\n 3.272727272727273,\n 2.164705882352941,\n 3.8,\n 3.0,\n 3.0372670807453415,\n 3.576923076923077,\n 2.857142857142857,\n 3.236842105263158,\n 3.3855421686746987,\n 3.7,\n 3.02803738317757,\n 3.609375,\n 3.3207547169811322,\n 3.0,\n 3.9696969696969697,\n 3.675675675675676,\n 3.6,\n 3.4,\n 3.4615384615384617,\n 3.4615384615384617,\n 3.6857142857142855,\n 3.8518518518518516,\n 3.1,\n 3.6417910447761193,\n 4.054607508532423,\n 3.7222222222222223,\n 3.622950819672131,\n 3.090909090909091,\n 3.5416666666666665,\n 3.2054794520547945,\n 3.5964912280701755,\n 3.3823529411764706,\n 3.1052631578947367,\n 3.1926605504587156,\n 3.3674698795180724,\n 3.876750700280112,\n 4.0625,\n 3.2439024390243905,\n 2.8484848484848486,\n 3.0476190476190474,\n 3.7829457364341086,\n 3.688073394495413,\n 4.086956521739131,\n 3.354285714285714,\n 4.4324324324324325,\n 3.265625,\n 3.0689655172413794,\n 4.296875,\n 3.7142857142857144,\n 4.0,\n 3.279503105590062,\n 2.0580357142857144,\n 3.7857142857142856,\n 3.466666666666667,\n 4.055555555555555,\n 2.953846153846154,\n 3.918918918918919,\n 3.8727272727272726,\n 3.7857142857142856,\n 2.788793103448276,\n 3.8666666666666667,\n 3.8461538461538463,\n 3.7037037037037037,\n 3.871794871794872,\n 3.1403508771929824,\n 3.3413654618473894,\n 3.6666666666666665,\n 3.017316017316017,\n 3.8,\n 3.127659574468085,\n 3.5090909090909093,\n 4.3076923076923075,\n 3.1320754716981134,\n 3.600558659217877,\n 2.6538461538461537,\n 3.3263598326359833,\n 4.0,\n 3.75,\n 4.038461538461538,\n 3.493333333333333,\n 3.090909090909091,\n 3.6656626506024095,\n 3.347985347985348,\n 3.642857142857143,\n 3.892156862745098,\n 4.130434782608695,\n 3.45,\n 4.423076923076923,\n 4.142857142857143,\n 3.5,\n 2.793103448275862,\n 2.9714285714285715,\n 3.7431693989071038,\n 3.84,\n 3.361111111111111,\n 3.4761904761904763,\n 4.071428571428571,\n 3.0,\n 4.338709677419355,\n 3.12,\n 4.005405405405406,\n 2.6,\n 3.613793103448276,\n 3.8260869565217392,\n 3.25,\n 3.1153846153846154,\n 3.546875,\n 3.7452830188679247,\n 3.5205479452054793,\n 2.96,\n 3.056179775280899,\n 3.925925925925926,\n 4.147058823529412,\n 3.86,\n 3.0434782608695654,\n 4.118279569892473,\n 3.192982456140351,\n 4.137055837563452,\n 3.903225806451613,\n 3.740740740740741,\n 2.735294117647059,\n 3.506172839506173,\n 3.129032258064516,\n 3.740740740740741,\n 3.0342465753424657,\n 4.286666666666667,\n 4.869565217391305,\n 4.529411764705882,\n 3.45933014354067,\n 3.4693877551020407,\n 2.975609756097561,\n 3.2119815668202767,\n 3.347826086956522,\n 3.423076923076923,\n 3.272727272727273,\n 3.4285714285714284,\n 3.8461538461538463,\n 3.23943661971831,\n 4.0,\n 4.363636363636363,\n 3.121495327102804,\n 3.82312925170068,\n 2.287878787878788,\n 2.7,\n 4.2631578947368425,\n 2.951923076923077,\n 2.872340425531915,\n 3.453531598513011,\n 3.4956521739130433,\n 3.380281690140845,\n 2.9,\n 3.823529411764706,\n 4.191011235955056,\n 4.190476190476191,\n 3.8271604938271606,\n 3.2686567164179103,\n 3.689655172413793,\n 3.426630434782609,\n 3.3204633204633205,\n 4.0510948905109485,\n 3.925373134328358,\n 3.7209302325581395,\n 3.3137254901960786,\n 3.3625,\n 3.9239766081871346,\n 4.3,\n 3.4355828220858897,\n 3.857142857142857,\n 4.127659574468085,\n 4.0,\n 3.4237288135593222,\n 3.636734693877551,\n 3.75,\n 2.9806629834254146,\n 3.962162162162162,\n 3.5,\n 3.525925925925926,\n 2.5555555555555554,\n 3.8548387096774195,\n 3.4489795918367347,\n 3.8529411764705883,\n 3.74468085106383,\n 3.275,\n 3.8048780487804876,\n 4.571428571428571,\n 3.554054054054054,\n 4.346153846153846,\n 3.1666666666666665,\n 3.806122448979592,\n 3.811320754716981,\n 3.522727272727273,\n 3.0869565217391304,\n 3.1153846153846154,\n 3.365930599369085,\n 3.5428571428571427,\n 3.349514563106796,\n 3.4700460829493087,\n 3.230769230769231,\n 3.272727272727273,\n 3.37007874015748,\n 4.148648648648648,\n 3.7560975609756095,\n 3.125,\n 3.3,\n 3.691666666666667,\n 4.6875,\n 3.693877551020408,\n 2.9682539682539684,\n 3.721311475409836,\n 3.966804979253112,\n 2.6467391304347827,\n 3.7011494252873565,\n 3.923076923076923,\n 3.7464788732394365,\n 3.375,\n 3.2685185185185186,\n 4.26530612244898,\n 3.457943925233645,\n 4.045454545454546,\n 4.265822784810126,\n 3.4107142857142856]"
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def calculate_user_rating_mu():\n",
    "    return [ \n",
    "        np.take(user_item_rating_array[user_index],\n",
    "        list(user_rating_map[user_index])).mean()\n",
    "        for user_index in range(len(user_rating_map))\n",
    "    ]\n",
    "user_rating_mean = calculate_user_rating_mu()\n",
    "user_rating_mean"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "943"
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(user_rating_map)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [],
   "source": [
    "def calculate_sim(user_index1, user_index2):\n",
    "    interset = list(user_rating_map[user_index1]& user_rating_map[user_index2])\n",
    "    if not interset: return 0.0 \n",
    "\n",
    "    v1 = np.take(\n",
    "        user_item_rating_array[user_index1],\n",
    "        interset\n",
    "    ) - user_rating_mean[user_index1]\n",
    "\n",
    "    v2 = np.take(\n",
    "        user_item_rating_array[user_index2],\n",
    "        interset\n",
    "    ) - user_rating_mean[user_index2]\n",
    "\n",
    "    sim = 1 - ssd.cosine(v1,v2)\n",
    "    if np.isnan(sim):\n",
    "        return 0.0\n",
    "    return sim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [],
   "source": [
    "user_sim_array = np.zeros(shape = (len(user_id_uique), len(user_id_uique)))\n",
    "from itertools import combinations\n",
    "for ui1, ui2 in combinations(range(len(user_id_uique) ), 2):\n",
    "    sim = calculate_sim(ui1, ui2)\n",
    "    user_sim_array[ui1][ui2], user_sim_array[ui2][ui1] = sim, sim"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "array([[ 0.        ,  0.5218078 ,  0.52512858, ...,  0.81332531,\n        -0.17784539,  0.29643726],\n       [ 0.5218078 ,  0.        ,  0.14002801, ...,  0.0327956 ,\n         0.24970539,  0.71248815],\n       [ 0.52512858,  0.14002801,  0.        , ...,  0.73994007,\n         0.51332107,  1.        ],\n       ...,\n       [ 0.81332531,  0.0327956 ,  0.73994007, ...,  0.        ,\n        -0.45148352,  0.01180366],\n       [-0.17784539,  0.24970539,  0.51332107, ..., -0.45148352,\n         0.        ,  0.4230873 ],\n       [ 0.29643726,  0.71248815,  1.        , ...,  0.01180366,\n         0.4230873 ,  0.        ]])"
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_sim_array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "array([[0.        , 0.        , 0.        , ..., 3.18582931, 3.7008185 ,\n        3.31555088],\n       [0.        , 3.52573006, 3.4781293 , ..., 3.30434783, 3.81933702,\n        3.4340694 ],\n       [3.0773674 , 2.9591157 , 2.64045279, ..., 3.49565217, 3.01933702,\n        0.        ],\n       ...,\n       [0.        , 3.80398663, 3.71419432, ..., 4.54110672, 4.06479156,\n        3.67952395],\n       [4.49835182, 4.1236582 , 3.9568226 , ..., 3.77017061, 4.2851598 ,\n        3.89989219],\n       [3.6688641 , 0.        , 2.88524803, ..., 2.91506211, 3.4300513 ,\n        3.04478369]])"
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_rating_predict = np.zeros_like(user_item_rating_array)\n",
    "\n",
    "for user_index in range(len(user_id_uique)):\n",
    "    for item_index in range(len(item_id_uique)):\n",
    "        if item_index not in user_rating_map[user_index]:\n",
    "            user_rating_index_v = np.where(\n",
    "                user_item_rating_array[:, item_index] > 0 \n",
    "            )[0]\n",
    "            if len(user_rating_index_v) ==0: continue\n",
    "            user_sim_v = np.take(\n",
    "                user_sim_array[user_index],\n",
    "                user_rating_index_v\n",
    "            )\n",
    "\n",
    "            user_sim_abs_sum = sum(abs(user_sim_v))\n",
    "            if user_sim_abs_sum ==0: continue\n",
    "            user_rating_v = np.take(\n",
    "                user_item_rating_array[:, item_index],\n",
    "                user_rating_index_v\n",
    "            ) - np.take(\n",
    "                user_rating_mean,\n",
    "                user_rating_index_v\n",
    "            )\n",
    "\n",
    "            predict_rating = np.dot(user_rating_v, user_sim_v)/ user_sim_abs_sum + user_rating_mean[user_index]\n",
    "            user_rating_predict[user_index,item_index] = predict_rating\n",
    "\n",
    "            \n",
    "# output the array \n",
    "# user_rating_predict = np.clip(user_rating_predict, 0, 5)\n",
    "user_rating_predict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>user_id</th>\n      <th>item_id</th>\n      <th>rating</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n      <td>6</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>1</td>\n      <td>10</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>1</td>\n      <td>12</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>1</td>\n      <td>14</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>1</td>\n      <td>17</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>19995</th>\n      <td>458</td>\n      <td>648</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>19996</th>\n      <td>458</td>\n      <td>1101</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>19997</th>\n      <td>459</td>\n      <td>934</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>19998</th>\n      <td>460</td>\n      <td>10</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>19999</th>\n      <td>462</td>\n      <td>682</td>\n      <td>5</td>\n    </tr>\n  </tbody>\n</table>\n<p>20000 rows × 3 columns</p>\n</div>",
      "text/plain": "       user_id  item_id  rating\n0            1        6       5\n1            1       10       3\n2            1       12       5\n3            1       14       5\n4            1       17       3\n...        ...      ...     ...\n19995      458      648       4\n19996      458     1101       4\n19997      459      934       3\n19998      460       10       3\n19999      462      682       5\n\n[20000 rows x 3 columns]"
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_test = pd.read_csv('./movielen_rating_test.base', sep = '\\t',names=['user_id', 'item_id', 'rating'], usecols=[0,1,2])\n",
    "df_test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "[0,\n 1,\n 2,\n 3,\n 4,\n 5,\n 6,\n 7,\n 8,\n 9,\n 10,\n 11,\n 12,\n 13,\n 14,\n 15,\n 16,\n 17,\n 18,\n 19,\n 20,\n 21,\n 22,\n 23,\n 24,\n 25,\n 26,\n 27,\n 28,\n 29,\n 30,\n 31,\n 32,\n 33,\n 34,\n 35,\n 36,\n 37,\n 38,\n 39,\n 40,\n 41,\n 42,\n 43,\n 44,\n 45,\n 46,\n 47,\n 48,\n 49,\n 50,\n 51,\n 52,\n 53,\n 54,\n 55,\n 56,\n 57,\n 58,\n 59,\n 60,\n 61,\n 62,\n 63,\n 64,\n 65,\n 66,\n 67,\n 68,\n 69,\n 70,\n 71,\n 72,\n 73,\n 74,\n 75,\n 76,\n 77,\n 78,\n 79,\n 80,\n 81,\n 82,\n 83,\n 84,\n 85,\n 86,\n 87,\n 88,\n 89,\n 90,\n 91,\n 92,\n 93,\n 94,\n 95,\n 96,\n 97,\n 98,\n 99,\n 100,\n 101,\n 102,\n 103,\n 104,\n 105,\n 106,\n 107,\n 108,\n 109,\n 110,\n 111,\n 112,\n 113,\n 114,\n 115,\n 116,\n 117,\n 118,\n 119,\n 120,\n 121,\n 122,\n 123,\n 124,\n 125,\n 126,\n 127,\n 128,\n 129,\n 130,\n 131,\n 132,\n 133,\n 134,\n 135,\n 136,\n 137,\n 138,\n 139,\n 140,\n 141,\n 142,\n 143,\n 144,\n 145,\n 146,\n 147,\n 148,\n 149,\n 150,\n 151,\n 152,\n 153,\n 154,\n 155,\n 156,\n 157,\n 158,\n 159,\n 160,\n 161,\n 162,\n 163,\n 164,\n 165,\n 166,\n 167,\n 168,\n 169,\n 170,\n 171,\n 172,\n 173,\n 174,\n 175,\n 176,\n 177,\n 178,\n 179,\n 180,\n 181,\n 182,\n 183,\n 184,\n 185,\n 186,\n 187,\n 188,\n 189,\n 190,\n 191,\n 192,\n 193,\n 194,\n 195,\n 196,\n 197,\n 198,\n 199,\n 200,\n 201,\n 202,\n 203,\n 204,\n 205,\n 206,\n 207,\n 208,\n 209,\n 210,\n 211,\n 212,\n 213,\n 214,\n 215,\n 216,\n 217,\n 218,\n 219,\n 220,\n 221,\n 222,\n 223,\n 224,\n 225,\n 226,\n 227,\n 228,\n 229,\n 230,\n 231,\n 232,\n 233,\n 234,\n 235,\n 236,\n 237,\n 238,\n 239,\n 240,\n 241,\n 242,\n 243,\n 244,\n 245,\n 246,\n 247,\n 248,\n 249,\n 250,\n 251,\n 252,\n 253,\n 254,\n 255,\n 256,\n 257,\n 258,\n 259,\n 260,\n 261,\n 262,\n 263,\n 264,\n 265,\n 266,\n 267,\n 268,\n 269,\n 270,\n 271,\n 272,\n 273,\n 274,\n 275,\n 276,\n 277,\n 278,\n 279,\n 280,\n 281,\n 282,\n 283,\n 284,\n 285,\n 286,\n 287,\n 288,\n 289,\n 290,\n 291,\n 292,\n 293,\n 294,\n 295,\n 296,\n 297,\n 298,\n 299,\n 300,\n 301,\n 302,\n 303,\n 304,\n 305,\n 306,\n 307,\n 308,\n 309,\n 310,\n 311,\n 312,\n 313,\n 314,\n 315,\n 316,\n 317,\n 318,\n 319,\n 320,\n 321,\n 322,\n 323,\n 324,\n 325,\n 326,\n 327,\n 328,\n 329,\n 330,\n 331,\n 332,\n 333,\n 334,\n 335,\n 336,\n 337,\n 338,\n 339,\n 340,\n 341,\n 342,\n 343,\n 344,\n 345,\n 346,\n 347,\n 348,\n 349,\n 350,\n 351,\n 352,\n 353,\n 354,\n 355,\n 356,\n 357,\n 358,\n 359,\n 360,\n 361,\n 362,\n 363,\n 364,\n 365,\n 366,\n 367,\n 368,\n 369,\n 370,\n 371,\n 372,\n 373,\n 374,\n 375,\n 376,\n 377,\n 378,\n 379,\n 380,\n 381,\n 382,\n 383,\n 384,\n 385,\n 386,\n 387,\n 388,\n 389,\n 390,\n 391,\n 392,\n 393,\n 394,\n 395,\n 396,\n 397,\n 398,\n 399,\n 400,\n 401,\n 402,\n 403,\n 404,\n 405,\n 406,\n 407,\n 408,\n 409,\n 410,\n 411,\n 412,\n 413,\n 414,\n 415,\n 416,\n 417,\n 418,\n 419,\n 420,\n 421,\n 422,\n 423,\n 424,\n 425,\n 426,\n 427,\n 428,\n 429,\n 430,\n 431,\n 432,\n 433,\n 434,\n 435,\n 436,\n 437,\n 438,\n 439,\n 441,\n 443,\n 444,\n 445,\n 446,\n 447,\n 448,\n 449,\n 450,\n 451,\n 452,\n 453,\n 454,\n 455,\n 456,\n 457,\n 458,\n 459,\n 461]"
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "user_test_unique = df_test.user_id.unique()\n",
    "valid_user_id_test = [user_id -1  for user_id in user_test_unique if user_id in user_index_map ]\n",
    "valid_user_id_test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>0</th>\n      <th>1</th>\n      <th>2</th>\n      <th>3</th>\n      <th>4</th>\n      <th>5</th>\n      <th>6</th>\n      <th>7</th>\n      <th>8</th>\n      <th>9</th>\n      <th>...</th>\n      <th>1640</th>\n      <th>1641</th>\n      <th>1642</th>\n      <th>1643</th>\n      <th>1644</th>\n      <th>1645</th>\n      <th>1646</th>\n      <th>1647</th>\n      <th>1648</th>\n      <th>1649</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>...</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>4.0</td>\n      <td>...</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>...</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>...</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>4.0</td>\n      <td>3.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>...</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>456</th>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>...</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>457</th>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>...</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>458</th>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>...</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>459</th>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>...</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n    </tr>\n    <tr>\n      <th>461</th>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>...</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n      <td>0.0</td>\n    </tr>\n  </tbody>\n</table>\n<p>459 rows × 1650 columns</p>\n</div>",
      "text/plain": "     0     1     2     3     4     5     6     7     8     9     ...  1640  \\\n0     0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0  ...   0.0   \n1     0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   4.0  ...   0.0   \n2     0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0  ...   0.0   \n3     0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0  ...   0.0   \n4     4.0   3.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0  ...   0.0   \n..    ...   ...   ...   ...   ...   ...   ...   ...   ...   ...  ...   ...   \n456   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0  ...   0.0   \n457   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0  ...   0.0   \n458   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0  ...   0.0   \n459   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0  ...   0.0   \n461   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0  ...   0.0   \n\n     1641  1642  1643  1644  1645  1646  1647  1648  1649  \n0     0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0  \n1     0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0  \n2     0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0  \n3     0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0  \n4     0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0  \n..    ...   ...   ...   ...   ...   ...   ...   ...   ...  \n456   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0  \n457   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0  \n458   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0  \n459   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0  \n461   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0   0.0  \n\n[459 rows x 1650 columns]"
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_user_item_rating_test = pd.DataFrame(np.zeros(shape = (len(valid_user_id_test), len(item_id_uique))))\n",
    "df_user_item_rating_test.index = valid_user_id_test\n",
    "for row in df_test.index:\n",
    "    data = df_test.iloc[row]\n",
    "    if data.user_id in user_rating_map and data.item_id in item_index_map:\n",
    "        df_user_item_rating_test[item_index_map[data.item_id]][user_index_map[data.user_id]] = data.rating\n",
    "\n",
    "df_user_item_rating_test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [],
   "source": [
    "def rmse(actual, predict):\n",
    "    acc_loss = np.sum(np.power(actual - predict, 2)); \n",
    "    acc_num = len(actual) \n",
    "    return np.sqrt(acc_loss/ acc_num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [],
   "source": [
    "actual = []\n",
    "predict = []\n",
    "for row in df_test.index:\n",
    "    data = df_test.iloc[row]\n",
    "    if data.user_id in user_rating_map and data.item_id in item_index_map:\n",
    "        curr_predict  = user_rating_predict[data.user_id][data.item_id]\n",
    "        if curr_predict:\n",
    "            predict.append(curr_predict)\n",
    "            actual.append(data.rating)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "1.2875399313505353"
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rmse(np.array(actual), np.clip(np.array(predict),1,5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "952,\n  1532,\n  1272,\n  973,\n  1028,\n  1338,\n  691,\n  672,\n  1278,\n  1628,\n  1631,\n  1634,\n  1630,\n  1626,\n  1602,\n  1498,\n  1179,\n  1230,\n  1258,\n  1205,\n  699],\n 893: [1574,\n  1570,\n  1514,\n  1609,\n  1598,\n  1317,\n  1624,\n  1230,\n  1163,\n  1168,\n  1576,\n  1601,\n  1606,\n  1592,\n  1607,\n  1334,\n  1344,\n  1166,\n  673,\n  1456,\n  1237,\n  1493,\n  1502,\n  1089,\n  1567,\n  1537,\n  1388,\n  1280,\n  1441,\n  1319,\n  952,\n  1272,\n  1481,\n  1447,\n  1475,\n  1464,\n  1466,\n  1476,\n  1479,\n  1473,\n  1450,\n  1477,\n  1443,\n  1480,\n  1478,\n  1487,\n  1452,\n  1484,\n  1485,\n  1486],\n 894: [1183,\n  1560,\n  1265,\n  1636,\n  1570,\n  1514,\n  1327,\n  1555,\n  1598,\n  417,\n  1624,\n  1623,\n  912,\n  1324,\n  1223,\n  1601,\n  1606,\n  1576,\n  1592,\n  1607,\n  1192,\n  1614,\n  1502,\n  1568,\n  1089,\n  1130,\n  1283,\n  1513,\n  1344,\n  1319,\n  679,\n  1243,\n  952,\n  1559,\n  173,\n  989,\n  1028,\n  1434,\n  972,\n  1504,\n  1521,\n  672,\n  91,\n  1146,\n  241,\n  1087,\n  959,\n  90,\n  973,\n  774],\n 895: [1362,\n  1366,\n  1644,\n  1327,\n  1506,\n  1514,\n  1230,\n  1610,\n  1624,\n  1646,\n  1324,\n  1607,\n  1592,\n  1576,\n  1606,\n  1601,\n  1317,\n  1599,\n  1502,\n  1269,\n  972,\n  953,\n  1089,\n  1567,\n  1232,\n  62,\n  241,\n  1564,\n  1319,\n  1235,\n  1534,\n  952,\n  1272,\n  1168,\n  90,\n  1302,\n  1303,\n  1337,\n  672,\n  1045,\n  673,\n  1239,\n  973,\n  679,\n  959,\n  1521,\n  357,\n  94,\n  1537,\n  910],\n 896: [1611,\n  1620,\n  1366,\n  1362,\n  1265,\n  1636,\n  1570,\n  1337,\n  1623,\n  1555,\n  1506,\n  982,\n  1327,\n  1573,\n  1556,\n  1514,\n  1345,\n  1165,\n  1610,\n  691,\n  674,\n  1624,\n  1168,\n  1639,\n  705,\n  1324,\n  1166,\n  1607,\n  1592,\n  1601,\n  1606,\n  1576,\n  1614,\n  1530,\n  1568,\n  1204,\n  1502,\n  786,\n  1179,\n  910,\n  1286,\n  1280,\n  1086,\n  1319,\n  1232,\n  1272,\n  1239,\n  1562,\n  1043,\n  1266],\n 897: [1554,\n  1611,\n  1337,\n  1366,\n  1362,\n  1636,\n  1298,\n  1570,\n  1098,\n  1520,\n  1500,\n  223,\n  1257,\n  255,\n  1327,\n  1609,\n  1184,\n  1573,\n  1629,\n  1621,\n  1345,\n  1514,\n  1412,\n  1517,\n  1524,\n  1255,\n  257,\n  1610,\n  1569,\n  1624,\n  910,\n  1168,\n  813,\n  1435,\n  1599,\n  1607,\n  1606,\n  1601,\n  1592,\n  1576,\n  1623,\n  1334,\n  1203,\n  1568,\n  1506,\n  1262,\n  1542,\n  1139,\n  1330,\n  1510],\n 898: [1618,\n  1366,\n  1362,\n  1619,\n  1337,\n  1621,\n  1520,\n  1623,\n  1327,\n  1642,\n  1569,\n  1514,\n  1610,\n  1598,\n  1639,\n  1317,\n  1555,\n  1324,\n  1388,\n  1607,\n  1601,\n  1576,\n  1592,\n  1606,\n  1587,\n  679,\n  1614,\n  1502,\n  1568,\n  910,\n  1089,\n  62,\n  1595,\n  1269,\n  1319,\n  952,\n  1272,\n  673,\n  90,\n  241,\n  972,\n  1045,\n  1504,\n  269,\n  1230,\n  1258,\n  1346,\n  1472,\n  1302,\n  1171],\n 899: [1554,\n  1265,\n  1623,\n  1432,\n  1555,\n  1327,\n  1514,\n  1610,\n  787,\n  173,\n  1260,\n  1624,\n  1317,\n  1598,\n  1639,\n  1612,\n  1324,\n  1592,\n  1576,\n  1607,\n  1606,\n  1601,\n  1614,\n  1334,\n  1237,\n  1393,\n  1533,\n  679,\n  1269,\n  1502,\n  1089,\n  1567,\n  1595,\n  1508,\n  1509,\n  1415,\n  124,\n  1319,\n  1521,\n  672,\n  1272,\n  1239,\n  910,\n  1621,\n  1166,\n  1303,\n  1279,\n  1045,\n  1344,\n  673],\n 900: [1618,\n  1366,\n  1362,\n  1636,\n  1619,\n  1506,\n  1228,\n  1610,\n  1225,\n  1624,\n  1179,\n  1168,\n  1346,\n  1324,\n  412,\n  1568,\n  1534,\n  934,\n  1422,\n  1502,\n  1187,\n  1553,\n  1564,\n  1542,\n  1604,\n  1590,\n  1582,\n  1580,\n  973,\n  1319,\n  1532,\n  1272,\n  1413,\n  672,\n  1239,\n  1330,\n  1394,\n  703,\n  637,\n  1237,\n  1302,\n  1602,\n  786,\n  1171,\n  1498,\n  1427,\n  1255,\n  725,\n  1098,\n  980],\n 901: [1560,\n  1362,\n  1366,\n  1265,\n  1644,\n  1621,\n  1337,\n  1563,\n  1146,\n  1514,\n  1598,\n  1610,\n  1168,\n  1230,\n  1260,\n  1624,\n  1595,\n  1118,\n  1585,\n  1388,\n  1646,\n  1284,\n  1298,\n  1601,\n  1576,\n  1606,\n  1607,\n  1592,\n  1553,\n  1225,\n  1534,\n  1317,\n  1584,\n  918,\n  1502,\n  1567,\n  173,\n  1614,\n  1280,\n  1370,\n  1319,\n  1599,\n  1555,\n  989,\n  953,\n  952,\n  1532,\n  1232,\n  1272,\n  1310],\n 902: [1611,\n  1574,\n  1265,\n  1636,\n  1365,\n  1644,\n  1570,\n  1230,\n  1514,\n  1327,\n  1317,\n  1624,\n  1345,\n  1521,\n  1225,\n  1646,\n  1324,\n  1606,\n  1601,\n  1576,\n  1592,\n  1607,\n  1334,\n  1568,\n  1502,\n  1533,\n  1089,\n  1319,\n  1280,\n  952,\n  1344,\n  1272,\n  1555,\n  976,\n  62,\n  910,\n  241,\n  1205,\n  989,\n  1045,\n  679,\n  1203,\n  1510,\n  1269,\n  1028,\n  472,\n  953,\n  1388,\n  959,\n  1585],\n 903: [1560,\n  1554,\n  1611,\n  1620,\n  1574,\n  1362,\n  1366,\n  1636,\n  1644,\n  1298,\n  1020,\n  1257,\n  1337,\n  572,\n  258,\n  696,\n  124,\n  1432,\n  890,\n  1254,\n  1520,\n  1242,\n  1500,\n  1549,\n  1376,\n  1559,\n  1165,\n  255,\n  1384,\n  1573,\n  1642,\n  1183,\n  22,\n  810,\n  1264,\n  1556,\n  1420,\n  629,\n  1166,\n  1513,\n  1598,\n  1389,\n  1430,\n  1412,\n  1536,\n  1613,\n  470,\n  1612,\n  1371,\n  1646],\n 904: [1560,\n  1620,\n  1636,\n  1506,\n  692,\n  1573,\n  1146,\n  1569,\n  1624,\n  173,\n  1613,\n  1612,\n  1324,\n  1592,\n  1576,\n  1607,\n  1601,\n  1606,\n  1568,\n  1502,\n  1518,\n  1537,\n  1344,\n  1598,\n  1585,\n  1225,\n  1319,\n  1555,\n  1283,\n  918,\n  674,\n  952,\n  1458,\n  1184,\n  1272,\n  1285,\n  1317,\n  1638,\n  62,\n  705,\n  1269,\n  1192,\n  1634,\n  1626,\n  1628,\n  1630,\n  1631,\n  972,\n  1562,\n  1045],\n 905: [1166,\n  1574,\n  1362,\n  1366,\n  1636,\n  1644,\n  1570,\n  1609,\n  1432,\n  787,\n  1517,\n  858,\n  1540,\n  976,\n  1642,\n  1598,\n  231,\n  1610,\n  1563,\n  637,\n  1299,\n  1121,\n  1146,\n  1639,\n  1400,\n  849,\n  1646,\n  864,\n  1601,\n  1607,\n  1493,\n  1592,\n  1606,\n  1576,\n  173,\n  793,\n  1334,\n  679,\n  1553,\n  1518,\n  1502,\n  1132,\n  1384,\n  1510,\n  1417,\n  1567,\n  1508,\n  1459,\n  1509,\n  1022],\n 906: [1618,\n  1554,\n  1184,\n  1611,\n  1620,\n  1574,\n  1366,\n  1362,\n  1265,\n  1636,\n  572,\n  1264,\n  1619,\n  1337,\n  1623,\n  1520,\n  255,\n  1500,\n  1556,\n  258,\n  1376,\n  1642,\n  1387,\n  1403,\n  796,\n  1595,\n  993,\n  1610,\n  1431,\n  223,\n  989,\n  1384,\n  1332,\n  1098,\n  1225,\n  1257,\n  1613,\n  1228,\n  1412,\n  1324,\n  1118,\n  1638,\n  1124,\n  1629,\n  1458,\n  1518,\n  1043,\n  1536,\n  1502,\n  1567],\n 907: [1560,\n  1554,\n  1611,\n  572,\n  1366,\n  1362,\n  1265,\n  1166,\n  1636,\n  1570,\n  1337,\n  696,\n  787,\n  255,\n  1555,\n  1514,\n  1506,\n  1610,\n  976,\n  1598,\n  1260,\n  1624,\n  666,\n  982,\n  1576,\n  1601,\n  1607,\n  1592,\n  1606,\n  1493,\n  1225,\n  1230,\n  1269,\n  1431,\n  1567,\n  1317,\n  1456,\n  849,\n  1502,\n  1343,\n  1609,\n  1441,\n  596,\n  1388,\n  1319,\n  412,\n  952,\n  1532,\n  256,\n  1272],\n 908: [1620,\n  1644,\n  1166,\n  1570,\n  1365,\n  1432,\n  1621,\n  1197,\n  1585,\n  1595,\n  1573,\n  1337,\n  666,\n  1118,\n  787,\n  1598,\n  1514,\n  1562,\n  1563,\n  1534,\n  621,\n  1610,\n  697,\n  1228,\n  629,\n  1624,\n  1623,\n  1431,\n  1613,\n  1612,\n  1168,\n  1646,\n  1324,\n  1536,\n  1592,\n  1607,\n  1606,\n  1576,\n  1601,\n  1282,\n  1198,\n  1638,\n  1555,\n  1225,\n  689,\n  1568,\n  989,\n  1502,\n  1089,\n  691],\n 909: [1365,\n  1644,\n  1569,\n  1573,\n  1327,\n  1514,\n  1599,\n  1506,\n  679,\n  1624,\n  1646,\n  1344,\n  1607,\n  1606,\n  1601,\n  1592,\n  1576,\n  1334,\n  1269,\n  1568,\n  1534,\n  1117,\n  1225,\n  1502,\n  1089,\n  1614,\n  1577,\n  1521,\n  972,\n  1537,\n  1319,\n  1284,\n  1553,\n  952,\n  1585,\n  1272,\n  1345,\n  1150,\n  1230,\n  241,\n  1303,\n  672,\n  1634,\n  1631,\n  1630,\n  1628,\n  1626,\n  1310,\n  443,\n  90],\n 910: [1618,\n  1620,\n  1265,\n  1636,\n  1365,\n  1644,\n  1166,\n  1619,\n  1621,\n  1403,\n  1555,\n  1540,\n  1642,\n  1514,\n  1577,\n  1327,\n  1163,\n  1472,\n  912,\n  804,\n  1598,\n  692,\n  1260,\n  1595,\n  1624,\n  1539,\n  1613,\n  1639,\n  673,\n  1510,\n  1646,\n  1324,\n  1320,\n  1322,\n  1606,\n  1576,\n  1592,\n  1601,\n  1607,\n  1197,\n  1287,\n  1587,\n  953,\n  62,\n  1556,\n  388,\n  1089,\n  1567,\n  188,\n  1509],\n 911: [1618,\n  1560,\n  1554,\n  1574,\n  1362,\n  1366,\n  1636,\n  1644,\n  1619,\n  1298,\n  1555,\n  1087,\n  258,\n  255,\n  1432,\n  1563,\n  1500,\n  124,\n  666,\n  811,\n  793,\n  1642,\n  256,\n  1514,\n  1346,\n  814,\n  22,\n  1610,\n  1577,\n  1598,\n  1118,\n  23,\n  1376,\n  989,\n  1624,\n  1609,\n  1190,\n  1387,\n  1284,\n  1612,\n  1595,\n  1513,\n  1646,\n  1607,\n  1592,\n  1576,\n  1606,\n  1601,\n  355,\n  910],\n 912: [1620,\n  1366,\n  1362,\n  1337,\n  1644,\n  1569,\n  1345,\n  1327,\n  1555,\n  1514,\n  1610,\n  1237,\n  1624,\n  1506,\n  1537,\n  1646,\n  1324,\n  1601,\n  1606,\n  1592,\n  1607,\n  1576,\n  1595,\n  1334,\n  1230,\n  62,\n  1502,\n  1269,\n  1089,\n  1567,\n  1629,\n  1534,\n  1585,\n  1599,\n  1563,\n  1319,\n  952,\n  1272,\n  1564,\n  972,\n  912,\n  1303,\n  1228,\n  1045,\n  1521,\n  1130,\n  1571,\n  443,\n  689,\n  1041],\n 913: [1611,\n  1620,\n  1412,\n  1184,\n  1365,\n  1574,\n  1265,\n  1636,\n  652,\n  1644,\n  1570,\n  976,\n  696,\n  1337,\n  1166,\n  1521,\n  1500,\n  1135,\n  572,\n  1540,\n  1577,\n  1573,\n  258,\n  1621,\n  629,\n  692,\n  1193,\n  1556,\n  1403,\n  1121,\n  1416,\n  1609,\n  1510,\n  1384,\n  1260,\n  813,\n  989,\n  1284,\n  1612,\n  1435,\n  1646,\n  124,\n  1345,\n  1327,\n  1334,\n  1419,\n  1622,\n  1209,\n  1225,\n  571],\n 914: [1560,\n  1554,\n  1620,\n  1574,\n  1366,\n  1362,\n  1644,\n  1569,\n  1562,\n  1570,\n  1098,\n  1520,\n  1555,\n  255,\n  918,\n  1540,\n  1365,\n  1514,\n  1287,\n  1327,\n  1228,\n  412,\n  1598,\n  1585,\n  1529,\n  1610,\n  62,\n  1537,\n  849,\n  1646,\n  1324,\n  1606,\n  1607,\n  1592,\n  1601,\n  1576,\n  1599,\n  1458,\n  1168,\n  1334,\n  1009,\n  1568,\n  1230,\n  1502,\n  1518,\n  1595,\n  1531,\n  1564,\n  1638,\n  1089],\n 915: [1362,\n  1366,\n  1570,\n  1237,\n  1514,\n  1327,\n  849,\n  1317,\n  1610,\n  1624,\n  1598,\n  1595,\n  1230,\n  1601,\n  1576,\n  1606,\n  1592,\n  1607,\n  1502,\n  673,\n  62,\n  1568,\n  1537,\n  1344,\n  1472,\n  1089,\n  241,\n  1571,\n  786,\n  674,\n  972,\n  1225,\n  173,\n  1319,\n  1564,\n  1521,\n  1280,\n  1493,\n  90,\n  952,\n  1272,\n  1456,\n  1281,\n  1569,\n  973,\n  1585,\n  67,\n  1269,\n  1045,\n  1285],\n 916: [1560,\n  1554,\n  1611,\n  1620,\n  1574,\n  1366,\n  1362,\n  1636,\n  1644,\n  258,\n  1570,\n  696,\n  1432,\n  1020,\n  1517,\n  1573,\n  1642,\n  666,\n  1524,\n  1559,\n  1514,\n  1327,\n  1598,\n  1536,\n  1610,\n  1393,\n  1510,\n  1533,\n  989,\n  1284,\n  1613,\n  1639,\n  1612,\n  1286,\n  1088,\n  1646,\n  1320,\n  1322,\n  1143,\n  572,\n  1317,\n  1576,\n  1592,\n  1607,\n  1606,\n  1601,\n  1614,\n  910,\n  1568,\n  637],\n 917: [1618,\n  1560,\n  1554,\n  1574,\n  1366,\n  1362,\n  1644,\n  1619,\n  1020,\n  258,\n  1570,\n  652,\n  1432,\n  1264,\n  1520,\n  255,\n  1197,\n  1500,\n  1412,\n  257,\n  1376,\n  1166,\n  256,\n  793,\n  1573,\n  1642,\n  1562,\n  1598,\n  596,\n  676,\n  615,\n  572,\n  621,\n  1610,\n  1338,\n  1228,\n  783,\n  1262,\n  1260,\n  1396,\n  1282,\n  602,\n  1613,\n  412,\n  1639,\n  1539,\n  1346,\n  1193,\n  1612,\n  1183],\n 918: [1620,\n  1362,\n  1366,\n  1636,\n  1621,\n  1327,\n  1555,\n  1610,\n  1598,\n  1599,\n  1624,\n  1230,\n  666,\n  1168,\n  1324,\n  1607,\n  1606,\n  1576,\n  1592,\n  1601,\n  674,\n  1225,\n  1388,\n  1087,\n  1568,\n  1283,\n  1098,\n  705,\n  1542,\n  786,\n  1502,\n  1298,\n  1564,\n  1319,\n  1280,\n  1506,\n  1272,\n  1595,\n  1370,\n  1631,\n  1630,\n  1634,\n  1626,\n  1628,\n  910,\n  412,\n  1045,\n  652,\n  62,\n  241],\n 919: [1560,\n  1554,\n  1611,\n  1620,\n  1362,\n  1366,\n  1098,\n  1298,\n  1520,\n  1500,\n  1621,\n  255,\n  1573,\n  1514,\n  1228,\n  1389,\n  412,\n  1412,\n  1624,\n  910,\n  1345,\n  1569,\n  1168,\n  1612,\n  1521,\n  1592,\n  1601,\n  1607,\n  1576,\n  1606,\n  223,\n  1334,\n  1568,\n  1502,\n  679,\n  1230,\n  1371,\n  1225,\n  1343,\n  1148,\n  793,\n  1319,\n  1435,\n  124,\n  1209,\n  1532,\n  672,\n  1272,\n  1577,\n  1599],\n 920: [1618,\n  1560,\n  1366,\n  1362,\n  1569,\n  1619,\n  1500,\n  1422,\n  1264,\n  1506,\n  412,\n  1514,\n  1563,\n  1555,\n  1624,\n  1239,\n  1556,\n  1098,\n  1324,\n  1204,\n  1521,\n  1179,\n  1345,\n  1568,\n  1502,\n  1089,\n  973,\n  1580,\n  1604,\n  1590,\n  1582,\n  1319,\n  459,\n  953,\n  62,\n  952,\n  1272,\n  1577,\n  1171,\n  1187,\n  1148,\n  1549,\n  1087,\n  1130,\n  1036,\n  1346,\n  1394,\n  1136,\n  988,\n  1235],\n 921: [1265,\n  1570,\n  1506,\n  1556,\n  1577,\n  1327,\n  1585,\n  1514,\n  12,\n  1610,\n  1260,\n  910,\n  1239,\n  1624,\n  1284,\n  1281,\n  1225,\n  673,\n  1569,\n  1607,\n  1592,\n  1601,\n  1576,\n  1606,\n  1564,\n  1237,\n  1283,\n  1089,\n  1567,\n  1502,\n  1204,\n  62,\n  1319,\n  459,\n  1553,\n  952,\n  1534,\n  1310,\n  1036,\n  1544,\n  1602,\n  1279,\n  966,\n  1214,\n  1302,\n  1456,\n  1630,\n  1628,\n  1634,\n  1626],\n 922: [1362,\n  1366,\n  1636,\n  1570,\n  1621,\n  1327,\n  1506,\n  1514,\n  1599,\n  1624,\n  912,\n  1639,\n  1225,\n  1601,\n  1576,\n  1592,\n  1607,\n  1606,\n  1614,\n  1502,\n  1568,\n  1542,\n  62,\n  679,\n  1280,\n  1598,\n  1319,\n  952,\n  1272,\n  1269,\n  1370,\n  972,\n  1230,\n  1628,\n  1626,\n  1630,\n  1631,\n  1634,\n  1179,\n  269,\n  1062,\n  1317,\n  1577,\n  1214,\n  959,\n  90,\n  91,\n  1184,\n  241,\n  1098],\n 923: [1560,\n  1197,\n  1362,\n  1366,\n  1265,\n  1636,\n  1337,\n  1644,\n  1569,\n  1520,\n  1555,\n  1563,\n  1621,\n  1327,\n  1573,\n  1345,\n  1577,\n  1514,\n  1610,\n  1585,\n  1260,\n  1624,\n  1540,\n  1646,\n  1287,\n  1607,\n  1606,\n  1601,\n  1576,\n  1592,\n  849,\n  989,\n  1118,\n  189,\n  1389,\n  953,\n  1567,\n  1599,\n  1512,\n  1534,\n  1553,\n  1319,\n  1609,\n  1504,\n  1532,\n  1272,\n  1317,\n  1531,\n  910,\n  438],\n 924: [1611,\n  1620,\n  1366,\n  1362,\n  1265,\n  1636,\n  1562,\n  1623,\n  1520,\n  1020,\n  1573,\n  1642,\n  1423,\n  1184,\n  412,\n  1571,\n  1555,\n  1506,\n  1584,\n  1536,\n  416,\n  417,\n  1639,\n  1324,\n  1320,\n  1322,\n  1598,\n  1568,\n  1638,\n  1098,\n  1518,\n  1089,\n  1513,\n  1325,\n  1523,\n  1604,\n  1582,\n  1590,\n  1580,\n  1502,\n  459,\n  800,\n  1023,\n  1532,\n  637,\n  1272,\n  189,\n  1621,\n  1458,\n  1137],\n 925: [1618,\n  1611,\n  1574,\n  258,\n  696,\n  1619,\n  1570,\n  1193,\n  256,\n  1132,\n  1432,\n  255,\n  1555,\n  912,\n  1598,\n  787,\n  1585,\n  629,\n  1610,\n  1337,\n  1260,\n  1146,\n  1228,\n  1624,\n  572,\n  1243,\n  1639,\n  1322,\n  1320,\n  1225,\n  1606,\n  1607,\n  1592,\n  1576,\n  1601,\n  1502,\n  1376,\n  1537,\n  1334,\n  1587,\n  1568,\n  1527,\n  1092,\n  1365,\n  1168,\n  1089,\n  1584,\n  173,\n  689,\n  1506],\n 926: [1618,\n  1554,\n  1620,\n  1366,\n  1362,\n  1265,\n  1636,\n  1619,\n  1337,\n  1500,\n  1562,\n  417,\n  1063,\n  412,\n  1264,\n  1623,\n  1327,\n  1422,\n  1260,\n  1330,\n  1613,\n  1639,\n  1324,\n  637,\n  1346,\n  1187,\n  1568,\n  1502,\n  1089,\n  886,\n  60,\n  1262,\n  705,\n  1582,\n  1604,\n  1580,\n  1590,\n  1274,\n  1319,\n  459,\n  1280,\n  1506,\n  1271,\n  1614,\n  1130,\n  1522,\n  1556,\n  1353,\n  1043,\n  1171],\n 927: [1560,\n  1554,\n  1574,\n  1644,\n  1570,\n  1432,\n  1337,\n  1500,\n  1559,\n  1298,\n  1573,\n  673,\n  1642,\n  1514,\n  1595,\n  679,\n  1146,\n  1327,\n  1458,\n  1610,\n  1598,\n  1166,\n  388,\n  1085,\n  1260,\n  910,\n  1624,\n  173,\n  918,\n  1530,\n  1613,\n  1135,\n  1612,\n  1230,\n  1646,\n  1431,\n  1322,\n  1320,\n  1456,\n  1576,\n  1592,\n  1607,\n  1606,\n  1601,\n  849,\n  1334,\n  1190,\n  1332,\n  1344,\n  1493],\n 928: [1554,\n  1574,\n  1644,\n  1374,\n  976,\n  1555,\n  1327,\n  1514,\n  1317,\n  696,\n  1610,\n  1624,\n  1639,\n  62,\n  1646,\n  1324,\n  1344,\n  1521,\n  1576,\n  1607,\n  1592,\n  1601,\n  1606,\n  1614,\n  1269,\n  1367,\n  1569,\n  1595,\n  1518,\n  1089,\n  1567,\n  1146,\n  1343,\n  674,\n  972,\n  1280,\n  1319,\n  1345,\n  771,\n  952,\n  1272,\n  1417,\n  1512,\n  1537,\n  173,\n  1431,\n  1045,\n  1134,\n  1403,\n  443],\n 929: [1618,\n  1554,\n  1620,\n  1362,\n  1366,\n  255,\n  1622,\n  1619,\n  1623,\n  1132,\n  1432,\n  1520,\n  912,\n  1422,\n  1555,\n  1022,\n  1106,\n  1629,\n  1536,\n  1388,\n  1337,\n  1610,\n  1237,\n  1533,\n  787,\n  783,\n  1260,\n  1624,\n  1118,\n  1261,\n  1472,\n  1585,\n  1613,\n  1612,\n  849,\n  673,\n  1507,\n  1324,\n  454,\n  1334,\n  1346,\n  1502,\n  1264,\n  1223,\n  1066,\n  256,\n  1518,\n  1274,\n  1243,\n  642],\n 930: [1184,\n  1620,\n  1365,\n  1574,\n  1265,\n  1570,\n  1432,\n  1237,\n  1510,\n  1327,\n  1393,\n  1598,\n  1317,\n  1225,\n  1168,\n  1613,\n  1639,\n  989,\n  1324,\n  416,\n  1576,\n  1592,\n  1601,\n  1606,\n  1607,\n  1236,\n  1334,\n  849,\n  1163,\n  1568,\n  1230,\n  1493,\n  1344,\n  1124,\n  1537,\n  1089,\n  1508,\n  1509,\n  1502,\n  1203,\n  1328,\n  1441,\n  679,\n  1368,\n  1319,\n  459,\n  173,\n  637,\n  1382,\n  1459],\n 931: [1560,\n  1365,\n  1573,\n  1642,\n  1514,\n  1327,\n  1623,\n  1610,\n  173,\n  1624,\n  1598,\n  679,\n  673,\n  1629,\n  1502,\n  1606,\n  1601,\n  1607,\n  1592,\n  1576,\n  1243,\n  1614,\n  1521,\n  1493,\n  1555,\n  1568,\n  1344,\n  1585,\n  241,\n  1279,\n  1533,\n  1319,\n  1537,\n  1368,\n  1504,\n  952,\n  770,\n  1272,\n  1480,\n  1490,\n  1487,\n  1464,\n  1486,\n  1488,\n  1447,\n  1489,\n  1473,\n  1478,\n  1477,\n  1466],\n 932: [1337,\n  1327,\n  1514,\n  1610,\n  1598,\n  1624,\n  673,\n  679,\n  1230,\n  1087,\n  1324,\n  1592,\n  1607,\n  1606,\n  1601,\n  1576,\n  1614,\n  1555,\n  1502,\n  1225,\n  241,\n  1344,\n  1521,\n  1237,\n  1236,\n  1319,\n  62,\n  1283,\n  952,\n  90,\n  1272,\n  1370,\n  972,\n  1269,\n  1130,\n  1506,\n  1564,\n  141,\n  1585,\n  1282,\n  953,\n  942,\n  484,\n  959,\n  94,\n  563,\n  1537,\n  1279,\n  486,\n  420],\n 933: [1560,\n  1183,\n  1574,\n  1265,\n  1636,\n  1563,\n  1520,\n  1555,\n  1573,\n  1549,\n  1514,\n  849,\n  1598,\n  1179,\n  1260,\n  1624,\n  1612,\n  1324,\n  1592,\n  1576,\n  1607,\n  1606,\n  1601,\n  673,\n  1334,\n  1623,\n  1502,\n  1284,\n  1237,\n  1166,\n  1281,\n  1614,\n  1567,\n  1109,\n  679,\n  973,\n  1319,\n  1515,\n  459,\n  1423,\n  1280,\n  1185,\n  1544,\n  952,\n  1264,\n  846,\n  1456,\n  672,\n  1628,\n  1630],\n 934: [1618,\n  1620,\n  1366,\n  1362,\n  1265,\n  1242,\n  1619,\n  1524,\n  1569,\n  1241,\n  1520,\n  1500,\n  1422,\n  1549,\n  793,\n  1595,\n  1506,\n  22,\n  1264,\n  1622,\n  1610,\n  1555,\n  1507,\n  1542,\n  1145,\n  1613,\n  1530,\n  1179,\n  1324,\n  1411,\n  1564,\n  1592,\n  1601,\n  1607,\n  1576,\n  1606,\n  1587,\n  1568,\n  574,\n  898,\n  1518,\n  783,\n  1089,\n  1523,\n  1509,\n  1508,\n  1281,\n  697,\n  849,\n  1512],\n 935: [1574,\n  1366,\n  1362,\n  1636,\n  1298,\n  1570,\n  1555,\n  1327,\n  1569,\n  1598,\n  1610,\n  1624,\n  1168,\n  689,\n  1502,\n  1576,\n  1592,\n  1606,\n  1601,\n  1607,\n  786,\n  1334,\n  1269,\n  1568,\n  1237,\n  1193,\n  674,\n  596,\n  1098,\n  1542,\n  412,\n  910,\n  1599,\n  1504,\n  1431,\n  1280,\n  1319,\n  1585,\n  241,\n  952,\n  696,\n  1272,\n  1028,\n  1537,\n  1370,\n  666,\n  1235,\n  60,\n  1628,\n  1630],\n 936: [1560,\n  1620,\n  1366,\n  1362,\n  1636,\n  1644,\n  1555,\n  1514,\n  1327,\n  1563,\n  1598,\n  1228,\n  1610,\n  1624,\n  1599,\n  1225,\n  1183,\n  1612,\n  1534,\n  1646,\n  1576,\n  1601,\n  1606,\n  1607,\n  1592,\n  1334,\n  1317,\n  1585,\n  1568,\n  1230,\n  1614,\n  1502,\n  679,\n  1232,\n  1319,\n  1621,\n  1571,\n  1203,\n  952,\n  1532,\n  674,\n  1628,\n  1634,\n  1631,\n  1630,\n  1626,\n  849,\n  677,\n  1045,\n  1564],\n 937: [1611,\n  1265,\n  1623,\n  1570,\n  1506,\n  1327,\n  1569,\n  1514,\n  1610,\n  1624,\n  1168,\n  912,\n  1639,\n  1345,\n  1607,\n  1606,\n  1576,\n  1592,\n  1601,\n  1542,\n  1223,\n  1346,\n  697,\n  1568,\n  1089,\n  1502,\n  62,\n  972,\n  1319,\n  1595,\n  1280,\n  1599,\n  952,\n  679,\n  1285,\n  1087,\n  1513,\n  1204,\n  966,\n  973,\n  1587,\n  1537,\n  1544,\n  1577,\n  1512,\n  1336,\n  774,\n  959,\n  1179,\n  1164],\n 938: [1618,\n  1620,\n  1574,\n  1362,\n  1366,\n  1562,\n  1619,\n  673,\n  1520,\n  637,\n  1598,\n  1346,\n  1563,\n  679,\n  1388,\n  1317,\n  1610,\n  1332,\n  1337,\n  1624,\n  1639,\n  1638,\n  1584,\n  1322,\n  1320,\n  1601,\n  1576,\n  1606,\n  1493,\n  1607,\n  1592,\n  1587,\n  62,\n  1264,\n  1568,\n  989,\n  1518,\n  1502,\n  231,\n  1368,\n  1533,\n  1124,\n  1043,\n  1403,\n  952,\n  1513,\n  674,\n  1271,\n  1285,\n  601],\n 939: [1618,\n  1611,\n  1362,\n  1366,\n  1619,\n  1337,\n  1569,\n  1573,\n  1514,\n  1610,\n  1595,\n  1639,\n  1264,\n  1324,\n  1322,\n  1320,\n  1606,\n  1592,\n  1576,\n  1607,\n  1601,\n  1587,\n  62,\n  953,\n  1568,\n  912,\n  1512,\n  1502,\n  1089,\n  1345,\n  1330,\n  1327,\n  952,\n  1272,\n  1269,\n  1413,\n  241,\n  1506,\n  1423,\n  90,\n  673,\n  1415,\n  1542,\n  1317,\n  1168,\n  1045,\n  1515,\n  1336,\n  271,\n  959],\n 940: [1183,\n  1560,\n  1574,\n  1265,\n  1636,\n  1644,\n  1570,\n  1562,\n  1623,\n  1179,\n  1642,\n  1327,\n  1584,\n  1610,\n  173,\n  1521,\n  1624,\n  1559,\n  1534,\n  1187,\n  1639,\n  1612,\n  1646,\n  1324,\n  1146,\n  12,\n  1600,\n  679,\n  1518,\n  1598,\n  1564,\n  1567,\n  1043,\n  1502,\n  1400,\n  1509,\n  1508,\n  1225,\n  1590,\n  1582,\n  1604,\n  1580,\n  1044,\n  637,\n  1319,\n  910,\n  1064,\n  1272,\n  1630,\n  1628],\n 941: [1560,\n  1366,\n  1362,\n  1636,\n  1563,\n  1621,\n  1520,\n  1327,\n  1514,\n  1281,\n  1598,\n  1317,\n  1260,\n  1624,\n  1595,\n  1324,\n  1537,\n  1334,\n  1043,\n  1533,\n  1568,\n  1183,\n  1502,\n  1553,\n  1531,\n  1284,\n  1609,\n  1571,\n  1262,\n  1582,\n  1590,\n  1580,\n  1604,\n  1319,\n  1280,\n  1283,\n  1036,\n  1532,\n  1600,\n  1599,\n  966,\n  1232,\n  1166,\n  1064,\n  1628,\n  1634,\n  1631,\n  1626,\n  1630,\n  1388],\n 942: [1362,\n  1366,\n  1570,\n  1549,\n  1542,\n  1184,\n  1327,\n  1506,\n  1610,\n  1624,\n  1639,\n  1324,\n  1576,\n  1592,\n  1601,\n  1606,\n  1607,\n  1179,\n  1502,\n  173,\n  666,\n  1087,\n  972,\n  1319,\n  1569,\n  1130,\n  1279,\n  952,\n  1272,\n  1345,\n  417,\n  1595,\n  1283,\n  786,\n  1370,\n  1602,\n  893,\n  141,\n  1258,\n  1394,\n  959,\n  562,\n  674,\n  90,\n  1556,\n  1040,\n  1534,\n  1548,\n  1225,\n  999]}"
     },
     "execution_count": 66,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def predict(predict_quantity):\n",
    "    predict_item_index_map = {}\n",
    "    for user_index in range(len(user_id_uique)):\n",
    "        predict_item_index_v = list(np.argsort(-user_rating_predict[user_index]))\n",
    "        predict_item_index_v = predict_item_index_v[:min(len(predict_item_index_v), predict_quantity)]\n",
    "        predict_item_index_map[user_index] = predict_item_index_v\n",
    "    return predict_item_index_map\n",
    "predict_item_map = predict(50)\n",
    "predict_item_map"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": "(0.0023965141612200436, 0.005424598086596311)"
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def calculate_precision_and_recall():\n",
    "    union_num = 0 \n",
    "    predict_num = 0 \n",
    "    test_num = 0 \n",
    "    for user_index in df_user_item_rating_test.index: \n",
    "        test_item_v = np.where(df_user_item_rating_test[user_index] >=3)[0].tolist()\n",
    "        union_num += len(set(predict_item_map[user_index])& set(test_item_v))\n",
    "        predict_num += len(predict_item_map[user_index])\n",
    "        test_num += len(test_item_v)\n",
    "    return union_num/predict_num , union_num/test_num\n",
    "calculate_precision_and_recall()"
   ]
  }
 ]
}